$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/hanwubookinfo/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'articleid', index: 'articleid', width: 50, key: true },
			{ label: '书名', name: 'articlename', index: 'articlename', width: 80 },
			{ label: '作者', name: 'author', index: 'author', width: 80 },
			{ label: '简介', name: 'intro', index: 'intro', width: 80 },
			{ label: '总字数', name: 'words', index: 'words', width: 80 },
            { label: '是否完结', name: 'fullflag', index: 'fullflag', width: 80 ,
                formatter: function(value, options, row) {
                    if (value === '1') {
                        return '<span class="label label-primary">完结</span>';
                    }
                    if (value === '0') {
                        return '<span class="label label-success">连载</span>';
                    }
                }},
            { label: '是否vip作品', name: 'isvip', index: 'isvip', width: 80 ,
                formatter: function(value, options, row) {
                    if (value === '1') {
                        return '<span class="label label-primary">VIP</span>';
                    }
                    if (value === '0') {
                        return '<span class="label label-success">免费</span>';
                    }
                }},
            { label: '状态', name: 'status', index: 'status', width: 80,
                formatter: function(value, options, row) {
                    if (value === 0) {
                        return '<span class="label label-primary">未导入</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-success">已导入</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-info">导入中</span>';
                    }
                    if (value === 3) {
                        return '<span class="label label-danger">导入失败</span>';
                    }
                } },
			{ label: '创建时间', name: 'createDate', index: 'create_date', width: 80 }, 			
			{ label: '导入时间', name: 'updateDate', index: 'update_date', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            articlename: null
        },
		showList: true,
		title: null,
		hanwuBookInfo: {},
        sels: [

        ],
        xels: [
            {value:"0",week:"未导入"},
            {value:"1",week:"已导入"},
            {value:"2",week:"导入中"},
            {value:"3",week:"导入失败"}
        ],
	},
    created: function () {
        $.ajax({
            url: baseURL + 'sys/category/findAll',
            type: "POST",
            contentType: "application/json",
            success: function (result) {
                vm.sels = result.list
            },
            error: function (error) {
                alert('数据加载失败!');
            }
        })
    },
	methods: {
        cateName: function () {
            vm.showList = true;
            vm.showDom = 'list'
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'status': this.hanwuBookInfo.status
                },
                page: page
            }).trigger("reloadGrid");
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.hanwuBookInfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.hanwuBookInfo.id == null ? "hanwubookinfo/save" : "hanwubookinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.hanwuBookInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        put: function (event){
            var url = "sys/hanwubookinfo/put"
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.hanwuBookInfo),
                success: function (r) {
                    if (r.code === 0) {
                        alert(r.msg, function () {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('删除第三方数据会删除图书，确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/hanwubookinfo/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert(r.msg, function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/hanwubookinfo/info/"+id, function(r){
                vm.hanwuBookInfo = r.hanwuBookInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {
                    'articlename': vm.q.articlename
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});